package cn.code.util;


import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;

@Component
public class ToolsUtil {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${app.bocha-sertch.api-key}")
    private String  apiKey;

    @Tool(description = "获取当前系统时间，可以调用此工具")
    public  String getCurrentTime(){
        return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
    }



    // 调用浏览器搜索工具
    @Tool(description = "调用浏览器搜索工具")
    public String search(String query) {
        // 设置请求头

        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer "+apiKey);  //认证头
        headers.set("Content-Type", "application/json");             //自定义头

        //设置body参数，并转成json字符串

        String content ="";
        HttpEntity<String> httpEntity = new HttpEntity<>(content, headers);

        String url = "https://api.bochaai.com/v1/web-search?query=" + query;
        return restTemplate.postForObject(url, httpEntity, String.class);
    }



}
